package com.bestcem.xm.ticket.controller.vo.ticket.config;

import com.bestcem.xm.ticket.constant.ColumnConstant;
import com.bestcem.xm.ticket.entity.mongo.TicketNotifyConfig;
import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.enums.TicketNotifyConfigEventEnum;
import com.bestcem.xm.ticket.grpc.client.dto.user.UserDTO;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.bestcem.xm.ticket.constant.ColumnConstant.TicketNotifyConfig.*;
import static com.bestcem.xm.ticket.constant.ColumnConstant.TicketNotifyConfig.RoleRegion.ID;
import static com.bestcem.xm.ticket.constant.ColumnConstant.TicketNotifyConfig.RoleRegion.*;

/**
 * TicketNotifyConfigVO
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/9 10:49
 */
@Data
public class TicketNotifyConfigVO {
    @JsonProperty(value = "id")
    private String id;

    /**
     * 通知用户信息
     */
    @JsonProperty(value = "notify_users")
    private List<UserDTO> notifyUsers;

    /**
     * 组织id
     */
    @JsonProperty(value = ORG_ID)
    private String orgId;

    /**
     * 工单配置id
     */
    @JsonProperty(value = TICKET_CONFIG_ID)
    private String ticketConfigId;

    /**
     *
     */
    @JsonProperty(value = IS_INSTANT)
    private Boolean instant;

    /**
     *
     */
    @JsonProperty(value = "status")
    private Integer status;

    /**
     *
     */
    @JsonProperty(value = MODE)
    private Integer mode;

    /**
     *
     */
    @JsonProperty(value = PERIOD)
    private List<Integer> period;

    /**
     * @see TicketNotifyConfigEventEnum
     */
    @JsonProperty(value = EVENT)
    private Integer event;

    /**
     * 跟进逾期前 hoursLimit 小时通知设置
     */
    @JsonProperty(value = HOURS_LIMIT)
    private Integer hoursLimit;

    /**
     *
     */
    @JsonProperty(value = HAS_RECEIVER)
    private Boolean hasReceiver;

    /**
     *
     */
    @JsonProperty(value = HAS_CC_LIST)
    private Boolean hasCcList;

    /**
     *
     */
    @JsonProperty(value = RECEIVER)
    private List<ReceiveUser> receivers;

    /**
     *
     */
    @JsonProperty(value = CC_LIST)
    private List<RoleRegion> ccList;

    /**
     * 自定义数据
     */
    @JsonProperty(value = CUSTOM_ATTR)
    private CustomAttr customAttr;

    /**
     * 通知方式
     */
    @JsonProperty(value = WAYS)
    private NotifySettingWayVO ways;

    /**
     * 邮箱id
     */
    @JsonProperty(value = SMTP_ID)
    private String smtpId;

    /**
     * 邮件主题
     */
    @JsonProperty(value = "emailSubject")
    private String emailSubject;
    /**
     * 邮件内容
     */
    @JsonProperty(value = "emailContent")
    private String emailContent;
    /**
     * 邮件模板id
     */
    @JsonProperty(value = "emailTemplateId")
    private String emailTemplateId;
    /**
     * 短信主题
     */
    @JsonProperty(value = "smsSubject")
    private String smsSubject;
    /**
     * 短信内容
     */
    @JsonProperty(value = "smsContent")
    private String smsContent;
    /**
     * 短信模板id
     */
    @JsonProperty(value = "smsTemplateId")
    private String smsTemplateId;

    @Data
    @JsonInclude
    public static class RoleRegion {

        @JsonProperty(value = REGION)
        private String region;

        @JsonProperty(value = ROLE_ID)
        private String roleId;

        /**
         * 邮箱地址
         */
        @JsonProperty(value = EMAIL)
        private String email;

        @JsonProperty(value = ID)
        private String id;

        /**
         * 用户名
         */
        @JsonProperty(value = "userName")
        private String userName;
        /**
         * 用户姓名
         */
        @JsonProperty(value = "name")
        private String name;

        /**
         * 用户手机号
         */
        @JsonProperty(value = "mobile")
        private String mobile;
        /**
         * 用户状态值
         */
        @JsonProperty(value = "status")
        private Integer status;

        @JsonProperty(value = "cc_filter")
        private Integer ccFilter;

        /**
         * 抄送人id
         */
        @JsonProperty(value = "cc_id")
        private String ccId;

        @Nullable
        public static RoleRegion getInstance(TicketNotifyConfig.RoleRegion entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            RoleRegion data = new RoleRegion();

            data.setRegion(entity.getRegion());
            data.setRoleId(entity.getRoleId());
            data.setEmail(entity.getEmail());
            data.setId(entity.getId());
            data.setUserName(entity.getUserName());
            data.setName(entity.getName());
            data.setMobile(entity.getMobile());
            data.setStatus(entity.getStatus());
            data.setCcFilter(entity.getCcFilter());
            data.setCcId(entity.getCcId());

            return data;
        }
    }

    @Data
    public static class ReceiveUser {

        /**
         * 邮箱地址
         */
        @JsonProperty(value = EMAIL)
        private String email;

        /**
         * 用户id
         */
        @JsonProperty(value = ID)
        private String id;

        /**
         * 用户手机号
         */
        @JsonProperty(value = "mobile")
        private String mobile;

        /**
         * 用户名
         */
        @JsonProperty(value = "userName")
        private String userName;

        /**
         * 区域
         */
        @JsonProperty(value = "region")
        private String region;

        /**
         * 角色id
         */
        @JsonProperty(value = "roleId")
        private String roleId;

        /**
         * followerFilter
         */
        @JsonProperty(value = "follower_filter")
        private int followerFilter;

        /**
         * followerId
         */
        @JsonProperty(value = "follower_id")
        private String followerId;

        @Nullable
        public static ReceiveUser getInstance(TicketNotifyConfig.ReceiveUser entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            ReceiveUser data = new ReceiveUser();

            data.setEmail(entity.getEmail());
            data.setId(entity.getId());
            data.setMobile(entity.getMobile());
            data.setUserName(entity.getUserName());
            data.setRegion(entity.getRegion());
            data.setRoleId(entity.getRoleId());
            data.setFollowerFilter(entity.getFollowerFilter());
            data.setFollowerId(entity.getFollowerId());

            return data;
        }
    }

    /**
     * 工单进度自定义参数, 分为两种 跟进/审核
     *
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/18 15:35
     */
    @Data
    public static class CustomAttr {
        private static final long serialVersionUID = 2808726620840986656L;

        // ==== 公共参数 ====
        /**
         * content
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.CONTENT)
        private String content;

        // ==== 跟进参数 ====
        /**
         * isCompleted
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.IS_COMPLETED)
        private Boolean isCompleted;

        /**
         * custom_fields
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.CUSTOM_FIELDS)
        private List<CustomField> customFields;

        @Data
        public static class CustomField {
            private static final long serialVersionUID = 2808726620840986656L;

            /**
             * field
             */
            @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.CustomField.FIELD)
            private String field;

            /**
             * text
             */
            @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.CustomField.TEXT)
            private String text;

            /**
             * value
             */
            @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.CustomField.VALUE)
            private String value;

            @Nullable
            public static CustomField getInstance(TicketRecord.CustomAttr.CustomField entity) {
                if (Objects.isNull(entity)) {
                    return null;
                }
                CustomField data = new CustomField();

                data.setField(entity.getField());
                data.setText(entity.getText());
                data.setValue(entity.getValue());

                return data;
            }
        }

        // ==== 审核参数 ====
        /**
         * isAgreed
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.IS_AGREED)
        private Boolean isAgreed;

        /**
         * auditHandle
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.AUDIT_HANDLE)
        private Integer auditHandle;

        /**
         * timeLimit
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.TIME_LIMIT)
        private Integer timeLimit;

        /**
         * timeLimitType
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.TIME_LIMIT_TYPE)
        private Integer timeLimitType;

        /**
         * transferFollowDeadline
         */
        @JsonProperty(value = ColumnConstant.TicketRecord.CustomAttr.TRANSFER_FOLLOW_DEADLINE)
        private Date transferFollowDeadline;

        @Nullable
        public static CustomAttr getInstance(TicketRecord.CustomAttr entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            CustomAttr data = new CustomAttr();

            data.setContent(entity.getContent());
            data.setIsCompleted(entity.getIsCompleted());
            data.setIsAgreed(entity.getIsAgreed());
            data.setAuditHandle(entity.getAuditHandle());
            data.setTimeLimit(entity.getTimeLimit());
            data.setTimeLimitType(entity.getTimeLimitType());
            data.setTransferFollowDeadline(entity.getTransferFollowDeadline());


            List<CustomField> customFieldList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(entity.getCustomFields())) {
                List<TicketRecord.CustomAttr.CustomField> customFields = entity.getCustomFields();
                for (TicketRecord.CustomAttr.CustomField customField : customFields) {
                    customFieldList.add(CustomField.getInstance(customField));
                }
            }
            data.setCustomFields(customFieldList);

            return data;
        }
    }

    @JsonProperty(value = "record_flag")
    private Integer recordFlag = 1;

    @JsonProperty("created_id")
    private String createdId;

    @JsonProperty("created_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @JsonProperty("updated_id")
    private String updatedId;

    @JsonProperty("updated_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedDate;
}
